<template>
  <div class="login-container">
    <lang-select class="international right-menu-item login-lang"></lang-select>
    <div class="login-bg">
      <el-form
        class="login-form"
        autocomplete="on"
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="left"
      >
        <!--<div class="title-container">-->
        <!--<h3 class="title">{{$t('login.title')}}</h3>-->
        <!--<lang-select class="set-language"></lang-select>-->
        <!--</div>-->
        <el-form-item prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user1" />
          </span>
          <el-input
            name="username"
            type="text"
            @blur="handleBlur"
            v-model="loginForm.username"
            autocomplete="off"
            :placeholder="$t('login.placeholder.username')"
          />
        </el-form-item>

        <el-form-item prop="password" style="margin-bottom: 12px;">
          <span class="svg-container">
            <svg-icon icon-class="password1" />
          </span>
          <el-input
            name="password"
            :type="passwordType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autocomplete="off"
            :placeholder="$t('login.placeholder.password')"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <div class="forget-account" @click="gotoPassword">{{ $t("login.buttonTips") }}</div>

        <div class="capy">
          <div id="captcha"></div>
        </div>

        <el-button
          id="btn_submit"
          type="primary"
          :disabled = dis
          style="width:100%;margin:45px 0 0 0;font-size: 16px;background-image: linear-gradient(to right, #32b798, #328fe1);"
          :loading="loading"
          @click.native.prevent="handleLogin"
        >{{ $t("login.logIn") }}</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
  import LangSelect from '@/components/LangSelect';
  import { validaRedis } from '@/api/login';
  export default {
    components: { LangSelect },
    name: 'login',
    data() {
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error(this.$t('login.ref.password')));
        } else {
          callback();
        }
      };
      return {
        dis: false,
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [
            {
              required: true,
              message: this.$t('login.ref.username'),
              trigger: 'blur'
            },
            {
              min: 3,
              max: 20,
              message: this.$t('login.ref.username1'),
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              trigger: 'blur',
              validator: validatePass
            }
          ]
        },
        passwordType: 'password',
        loading: false,
        showDialog: false
      };
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = '';
        } else {
          this.passwordType = 'password';
        }
      },

      handleBlur(){
          validaRedis(this.loginForm.username).then(res => {
                if(res.status === 201){ 
                    this.$message({
                      message: res.message, 
                       type: 'error'
                  });
                  this.dis = true;
                  return false;
                }
                  this.dis = false;
          })
                 
      },

      handleLogin() {
        const this_temp = this;
        this_temp.$refs.loginForm.validate(valid => {
          if (valid) {
            const postdata = {
              public_key: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/s1i82gaMKVMlngK+0FoonH2EEKmqXs+yYzSV3ad7HSSjlGDCxg+mg1IHceVEqC7DXc4W7wOSIIHMlbTDrvPTlQ4Z4NpIiYV9ePAxR0Qp0l8IrFN3M8V5Eon/je77z6POdzCaaxfdgmM/1ZSB1oco14X6VRVuxng+DHTXaQszDwIDAQAB',
              efcaptcha_data: $('#efcaptcha_data').val(),
              challenge_key: $('#challenge_key').val()
            };
            $.ajax({
              url: 'http://52.189.196.31/efcaptcha/api/v01/verify',
              type: 'POST',
              dataType: 'json',
              data: JSON.stringify(postdata),
              contentType: 'application/json',
              success() {
                console.log('圖形驗證成功');
                // this_temp.$refs.loginForm.validate(valid => {
                //   if (valid) {
                this_temp.loading = true;
                this_temp.$store
                  .dispatch('LoginByUsername', this_temp.loginForm)
                  .then(() => {
                    this_temp.loading = false;
                    this_temp.$router.push({
                      path: '/'
                    });
                    // this_temp.showDialog = true;
                  })
                  .catch(() => {
                    this_temp.loading = false;
                  });
                //    } else {
                //     return false;
                //   }
                // });
              },
              error(XMLHttpRequest) {
                console.log(XMLHttpRequest.status);
                switch (XMLHttpRequest.status) {
                  case 500:
                    console.log(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
                    alert('圖形驗證失敗');
                    location.reload();
                    break;
                  default:
                    alert('請完成圖形驗證');
                    console.log(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
                }
                return false;
              }
            });
          } else {
            return false;
          }
        });
      },
      gotoPassword() {
        this.$router.push({ path: '/password' });
      },
      afterQRScan() {
        // const hash = window.location.hash.slice(1)
        // const hashObj = getQueryObject(hash)
        // const originUrl = window.location.origin
        // history.replaceState({}, '', originUrl)
        // const codeMap = {
        //   wechat: 'code',
        //   tencent: 'code'
        // }
        // const codeName = hashObj[codeMap[this.auth_type]]
        // if (!codeName) {
        //   alert('第三方登录失败')
        // } else {
        //   this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
        //     this.$router.push({ path: '/' })
        //   })
        // }
      }
    },
    created() {
      // window.addEventListener('hashchange', this.afterQRScan)
    },
    destroyed() {
      // window.removeEventListener('hashchange', this.afterQRScan)
    }
  };
</script>




<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-lang {
  position: absolute;
  right: 150px;
  top: 50%;
  margin: -335px 0 0 0;
}
.login-container {
  background: url("../../assets/images/loginBg.jpg");
  background-size: cover;
  .login-bg {
    position: absolute;
    right: 150px;
    top: 48%;
    width: 640px;
    height: 690px;
    padding: 0px 35px 15px 35px;
    margin: -285px 0 0 0;
    background: url("../../assets/images/loginDl.png");
    background-size: cover;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0);
    border-bottom: 1px solid #ccc;
    border-radius: 0px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  @include relative;
  height: 100vh;
  background-color: $bg;
  .forget-account {
    text-align: right;
    color: #c5cdd3;
    margin: 12px 0;
    cursor: pointer;
    font-size: 12px;
  }
  .login-form {
    width: 520px;
    padding: 50px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    font-size: 18px;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
</style>

<style scoped>
.capy {
  height: 200px;
}
</style>
